package adger.me.yili.utils;

import android.os.Handler;
import android.os.Looper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by heyue on 16/1/29.
 */
public class OkHttpManger {
    private Handler mHander;
    private static OkHttpManger mInstance;
    private OkHttpClient mOkHttpClient;
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    private OkHttpManger()
    {
        mOkHttpClient = new OkHttpClient();
        //cookie enabled
//        mOkHttpClient.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
        mHander = new Handler(Looper.getMainLooper());
    }
    public static OkHttpManger getInstance()
    {
        if (mInstance == null)
        {
            synchronized (OkHttpManger.class)
            {
                if (mInstance == null)
                {
                    mInstance = new OkHttpManger();
                }
            }
        }
        return mInstance;
    }
    /**
     * 异步的post请求
     *
     * @param url
     * @param callback
     */
    public void postAsyn(String url, String bodyStr, final OkResultCallback callback)
    {
//        Request request = buildPostRequest(url, params);
        RequestBody body = RequestBody.create(JSON, bodyStr);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        deliveryResult(request,callback);
    }

    public static abstract class OkResultCallback
    {
        public abstract void onError(Call call, Exception e);

        public abstract void onResponse(Call call,String backStr) throws  IOException ;
    }

    private void deliveryResult( Request request,final OkResultCallback callback)
    {
//        client.newCall(request).enqueue(callBack);
        mOkHttpClient.newCall(request).enqueue(new Callback()
        {
            @Override
            public void onFailure(Call call, IOException e) {
                sendFailedStringCallback(call, e, callback);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                sendSuccessResultCallback(call,response,callback);
            }
        });
    }
    private void sendSuccessResultCallback(final Call call,final Response response, final OkResultCallback callback)
    {

        mHander.post(new Runnable()
        {
            @Override
            public void run()
            {
                if (callback != null)
                {
                    try {
                        callback.onResponse(call, response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    private void sendFailedStringCallback(final Call call, final Exception e, final OkResultCallback callback)
    {
        mHander.post(new Runnable()
        {
            @Override
            public void run()
            {
                if (callback != null)
                    callback.onError(call, e);
            }
        });
    }

}
